package aiball

import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import components.GlobalConstants;
import components.SecurityParser;
import components.PlayerCompiler;

class CompilerService {

	static final String TEAMS_BASE_DIR = GlobalConstants.PATH_TEAMS;
	def Team findTeamToPlayer(Player player) {
		
		Team tmp = Team.findByPlayer1(player);
		if(tmp != null) {
			return tmp;
		}
		tmp = Team.findByPlayer2(player);
		if(tmp != null) {
			return tmp;
		}
		tmp = Team.findByPlayer3(player);
		if(tmp != null) {
			return tmp;
		}
		tmp = Team.findByPlayer4(player);
		if(tmp != null) {
			return tmp;
		}
		tmp = Team.findByPlayer5(player);
		if(tmp != null) {
			return tmp;
		}
		
	}
	
public void writeAnXMLFileW3C(Document d, File file) throws IOException,
      TransformerFactoryConfigurationError, TransformerException {
 
    /* Create a transformation factory */
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setAttribute("indent-number", new Integer(2));
 
    /* Create and configure a transformation factory */
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    t.setOutputProperty(OutputKeys.STANDALONE, "yes");
 
    /* Write out and transform the document as we do so */
    StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
    t.transform(new DOMSource(d), result);
  }
	
	
	def generateLineupXML(Team team)
	{
	
		def pc = new PlayerCompiler();
		def teamPath = TEAMS_BASE_DIR + "/" + team.name;
		
		
		def model = "";
		
		switch(team.getModel())
		{
			case 1: 
			model = "CYAN";
			break;
			
			case 2:
			model = "MAGENTA";
			break;
			
			case 3:
			model = "RED";
			break;
			
			case 4:
			model = "YELLOW";
			break;
			
			case 5:
			model = "WHITE";
			break;
			
			case 6:
			model = "BLUE";
			break;
			
			case 7:
			model = "BLACK";
			break;
			
			case 8:
			model = "GREEN";
			break;
			
			case 9:
			model = "MAGENTA";
			break;
		}
		
		
		def classNameP1 = (pc.getClassName(team.getPlayer1().getSourceCode()));
		def classNameP2 = (pc.getClassName(team.getPlayer2().getSourceCode()));
		def classNameP3 = (pc.getClassName(team.getPlayer3().getSourceCode()));
		def classNameP4 = (pc.getClassName(team.getPlayer4().getSourceCode()));
		def classNameP5 = (pc.getClassName(team.getPlayer5().getSourceCode()));
		
		
		if( (new File(teamPath + "/" + classNameP1 + ".class")).exists() && (new File(teamPath + "/" + classNameP2 + ".class")).exists() && (new File(teamPath + "/" + classNameP3 + ".class")).exists() && (new File(teamPath + "/" + classNameP4 + ".class")).exists() && (new File(teamPath + "/" + classNameP5 + ".class")).exists() )
		{
			
			
		Document lineupXML = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		
		//Lineup Tag
		Element lineupElement = lineupXML.createElement("lineup");
		lineupElement.setAttribute("team", team.getName());
		lineupElement.setAttribute("jersey", model);
		lineupXML.appendChild(lineupElement);
		
		//Player 1
		Element p1Element = lineupXML.createElement("player");
		p1Element.setAttribute("name", team.getPlayer1().getName());
		p1Element.setAttribute("no", ""+team.getPlayer1().id);
		p1Element.setAttribute("class", classNameP1);
		lineupElement.appendChild(p1Element);
		
		//Player 2
		Element p2Element = lineupXML.createElement("player");
		p2Element.setAttribute("name", team.getPlayer2().getName());
		p2Element.setAttribute("no", ""+team.getPlayer2().id);
		p2Element.setAttribute("class", classNameP2);
		lineupElement.appendChild(p2Element);
		
		//Player 3
		Element p3Element = lineupXML.createElement("player");
		p3Element.setAttribute("name", team.getPlayer3().getName());
		p3Element.setAttribute("no", ""+team.getPlayer3().id);
		p3Element.setAttribute("class", classNameP3);
		lineupElement.appendChild(p3Element);
		
		//Player 4
		Element p4Element = lineupXML.createElement("player");
		p4Element.setAttribute("name", team.getPlayer4().getName());
		p4Element.setAttribute("no", ""+team.getPlayer4().id);
		p4Element.setAttribute("class", classNameP4);
		lineupElement.appendChild(p4Element);
		
		//Player 5
		Element p5Element = lineupXML.createElement("player");
		p5Element.setAttribute("name", team.getPlayer5().getName());
		p5Element.setAttribute("no", ""+team.getPlayer5().id);
		p5Element.setAttribute("class", classNameP5);
		lineupElement.appendChild(p5Element);
		
		try
		{
			(new File(teamPath + "/lineup.xml")).delete();
		}
		catch(Exception e)
		{
			
		}
		
		writeAnXMLFileW3C(lineupXML, new File(teamPath + "/lineup.xml"));
			
		
		
		
		}
		
		
		
	}
	

	def String compileAndParse(Player player,String code) {

		def String rv = "";
		
		Team tmp;

		def p = new SecurityParser();
		
		rv += p.checkCode(code);

		if(rv.isEmpty()) {

			tmp = findTeamToPlayer(player);

			def outputPath = TEAMS_BASE_DIR + "/" + tmp.name;

			(new File(outputPath)).mkdirs();

			def c = new PlayerCompiler();
			rv += c.Compile(code, outputPath);
		}

//		if(rv.isEmpty())
//		{
//			generateLineupXML(tmp);
//		}

		return rv;
	}
}
